
namespace Ann
{
  using System;
  using System.Collections.Generic;
  using System.Text;
  public static class Utils
  {
    #region For ulong
    public static ulong Increase(ref ulong quantity, ulong share)
    {
      ulong former = quantity;
      quantity += share;
      if (former > quantity)
      {
        quantity = ulong.MaxValue;

        share = quantity - former;
      }
      return share;
    }

    public static ulong Decrease(ref ulong quantity, ulong share)
    {
      ulong former = quantity;
      quantity -= share;
      if (former < quantity)
      {
        quantity = ulong.MinValue;

        share = former - quantity;
      }
      return share;
    }

    public static ulong Recrease(ref ulong quantity, int share)
    {
      return share >= 0 ?
        Increase(ref quantity, (ulong)(share)) :
        Decrease(ref quantity, (ulong)(-share));
    }

    public static ulong Transfer(ref ulong fromQuantity, ref ulong toQuantity, ulong share)
    {
      return Increase(ref toQuantity, Decrease(ref fromQuantity, share));
    }
    #endregion
    #region For uint
    public static uint Increase(ref uint quantity, uint share)
    {
      uint former = quantity;
      quantity += share;
      if (former > quantity)
      {
        quantity = uint.MaxValue;

        share = quantity - former;
      }
      return share;
    }

    public static uint Decrease(ref uint quantity, uint share)
    {
      uint former = quantity;
      quantity -= share;
      if (former < quantity)
      {
        quantity = uint.MinValue;

        share = former - quantity;
      }
      return share;
    }

    public static uint Recrease(ref uint quantity, int share)
    {
      return share >= 0 ?
        Increase(ref quantity, (uint)(share)) :
        Decrease(ref quantity, (uint)(-share));
    }

    public static uint Transfer(ref uint fromQuantity, ref uint toQuantity, uint share)
    {
      return Increase(ref toQuantity, Decrease(ref fromQuantity, share));
    }
    #endregion
  }
}
